package com.xncoding.valid.controller;

import com.alibaba.fastjson.JSON;
import com.xncoding.valid.Resolver.Pack;
import com.xncoding.valid.dto.Message;
import com.xncoding.valid.dto.UserUpdateGenderDTO;
import com.xncoding.valid.dto.Users;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/users")
@Validated
public class UserController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * @GenderCheck注解测试
     * @param updateGenderDTO
     * @return
     */
    @PostMapping("/update_gender")
    public String updateGender(@Valid UserUpdateGenderDTO updateGenderDTO) {
        logger.info("[updateGender][updateGenderDTO: {}]", updateGenderDTO);
        return "性别更新成功";
    }
    /**
     * 方法非常简单还是用的那些常用的类，@RequestBody接收请求body中的内容
     * 从从请求中读取数据读取 aa|bb|cc|dd  格式数据
     * application/pack;charset=UTF-8
      */
    @PostMapping("/testmsg")
    public Object testmsg(@RequestBody Users user) {
        System.out.println(JSON.toJSONString(user)) ;
        return user ;
    }

    /**
     * Content-Type为application/fm。
     * 读取name：jack，age：20的数据
     * @param user
     * @return
     */
    @PostMapping("/save")
    public Users save(@RequestBody Users user) {
        System.out.println("接受到内容：" + user) ;
        return user ;
    }

    @PostMapping("/resolver")
    public Object resolver(@Pack Users user) {
        System.out.println("自定义参数解析器处理结果：" + user) ;
        return user ;
    }

    @GetMapping(value = "/index")
    public Object index() {
        return new Message("标题", "内容") ;
    }
}